package com.ctcemti.mdm.project.sys.service.impl;

import com.ctcemti.mdm.common.util.RedisUtil;
import com.ctcemti.mdm.project.sys.dao.CompanyMapper;
import com.ctcemti.mdm.project.sys.dao.ProjectMapper;
import com.ctcemti.mdm.project.sys.dao.RelOrganizationProjectMapper;
import com.ctcemti.mdm.project.sys.dao.RelPersonOrgMapper;
import com.ctcemti.mdm.project.sys.entity.OrgOrganization;
import com.ctcemti.mdm.project.sys.entity.RelPersonOrg;
import com.ctcemti.mdm.project.sys.service.ICommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommonService implements ICommonService {

    @Autowired
    ProjectMapper projectMapper;

    @Autowired
    RelOrganizationProjectMapper relOrganizationProjectMapper;

    @Autowired
    RelPersonOrgMapper relPersonOrgMapper;

    @Autowired
    CompanyMapper companyMapper;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public String insertComRelationToRedisByPersonId(String personId) {
        //先删除原有的内容
        redisUtil.del(personId);
        List<OrgOrganization> result = new ArrayList<>();
        //根据人员id获取对应的组织以及上一层组织类型，从而根据人所在的组织来获取对应的项目
        //人如果是在项目部下的部门中，则只能获取自己所在的项目部
        //获取人所在组织
        RelPersonOrg relPersonOrg = new RelPersonOrg();
        relPersonOrg.setPersonId(personId);
        //获取所有人员与组织的对应关系
        List<RelPersonOrg> relPersonOrgs= relPersonOrgMapper.select(relPersonOrg);
        //循环获取所有组织
        if(relPersonOrgs != null && relPersonOrgs.size()>0){
            for(RelPersonOrg temp:relPersonOrgs){
                OrgOrganization orgTemp = new OrgOrganization();
                orgTemp = companyMapper.selectByPrimaryKey(temp.getOrgId());
                if(orgTemp == null){
                    continue;
                }
                OrgOrganization orgParent = new OrgOrganization();
                orgParent = companyMapper.selectByPrimaryKey(orgTemp.getParentId());
                //
                if(orgParent == null){
                    continue;
                }
               /* if(orgParent.getParentId().equals("")){
                    //存入redis，内容为all表示所有组织都有权限
                }*/
                result.add(orgParent);
                //判断组织类型
                switch(orgParent.getType()){
                    //公司
                    case 3: case 1:
                        //获取上级组织公司
                        OrgOrganization parentOrg = new OrgOrganization();
                        parentOrg.setId(orgParent.getParentId());
                        parentOrg = companyMapper.selectByPrimaryKey(parentOrg);
                        if(parentOrg == null){
                            continue;
                        }
                        //result.add(parentOrg);
                        //判断是否是根节点，如果是根节点全部存入
                        if(parentOrg.getParentId()==null || parentOrg.getParentId().equals("")){
                            List<OrgOrganization> all = new ArrayList<>();
                            all = companyMapper.selectAll();
                            //存入一个空的数组代表全部数据
                            redisUtil.set(personId,all);
                            return "";
                        }
                        //获取所有虚拟组织
                        OrgOrganization paramForSecondOrg = new OrgOrganization();
                        paramForSecondOrg.setParentId(parentOrg.getId());
                        //List<OrgOrganization> secondOrgs = companyMapper.select(paramForSecondOrg);该方法无法排序，因（singer）--bxf改

                        Example example = new Example(OrgOrganization.class);
                        example.setOrderByClause("sort desc");
                        Example.Criteria criteria = example.createCriteria();
                        criteria.andEqualTo("parentId",parentOrg.getId());
                        List<OrgOrganization> secondOrgs = companyMapper.selectByExample(example);

                        if(secondOrgs == null || secondOrgs.size() == 0){
                            continue;
                        }
                        for(OrgOrganization t:secondOrgs){
                            //判断该虚拟组织是否包含项目
                            String count = companyMapper.countProject(t.getId());
                            if(Integer.parseInt(count)>0){
                                result.add(t);
                                //获取所有项目存入redis
                                OrgOrganization paramForProjects = new OrgOrganization();
                                paramForProjects.setParentId(t.getId());
                                result.addAll(companyMapper.select(paramForProjects));
                            }
                        }
                        break;
                        //项目
                    case 4:
                        //获取父级虚拟组织和公司存入redis
                        OrgOrganization vOrg = new OrgOrganization();
                        vOrg.setId(orgParent.getParentId());
                        vOrg = companyMapper.selectByPrimaryKey(vOrg);
                        result.add(vOrg);
                        //获取父级组织
                        OrgOrganization pOrg = new OrgOrganization();
                        pOrg.setId(vOrg.getParentId());
                        pOrg = companyMapper.selectByPrimaryKey(pOrg);
                        result.add(pOrg);
                        break;
                }
            }
            //存入redis
            redisUtil.set(personId,result);
        }
        return "";
    }


    @Override
    public String insertComRelationToRedisByPersonIdForAdministrator(String personId) {
        //先删除原有的内容
        redisUtil.del(personId);
        List<OrgOrganization> result = new ArrayList<>();
        Example example = new Example(OrgOrganization.class);
        example.setOrderByClause("sort asc");
        List<OrgOrganization> all = companyMapper.selectAllForSort();
        //存入一个空的数组代表全部数据
        redisUtil.set(personId,all);
        return "";
    }
}
